package com.example.admin.controller.candidates;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.admin.config.OSSUploadUtils;
import com.example.airobotinterviewrecord.util.Result;
import com.example.candiates.domain.InterviewCandidates;
import com.example.candiates.service.InterviewCandidatesService;
import com.example.candiates.service.ResumeStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/InterviewCandidate")
public class InterviewCandidateController {

    @Autowired
    private InterviewCandidatesService interviewCandidatesService;

//    @Autowired
//    private ResumeStorageService resumeStorageService;

    @Autowired
    private OSSUploadUtils ossUploadUtils;

    /**
     * 添加候选人,
     * @param candidate ·······················································
     * @return
     */
    @PostMapping("/addCandidate")
    public Result addCandidate(@RequestBody InterviewCandidates candidate) {
        return Result.success(interviewCandidatesService.save(candidate));
    }

    /**
     * 删除候选人
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteCandidate(@PathVariable Integer id) {
        return Result.success(interviewCandidatesService.removeById(id));
    }

    /**
     * 修改候选人信息
     * @param id
     * @param candidate
     * @return
     */
    @PutMapping("/{id}")
    public Result updateCandidate(@PathVariable Integer id,
                                               @RequestBody InterviewCandidates candidate) {
        candidate.setId(id);
        return Result.success(interviewCandidatesService.updateById(candidate));
    }

    /**
     * 查询候选人
     * @param pageNum
     * @param pageSize
     * @param interviewCandidates
     * @return
     */
    @GetMapping("/getList")
    public Result<Page<InterviewCandidates>> getAllCandidates(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            InterviewCandidates interviewCandidates) {
        // 构建分页对象
        Page<InterviewCandidates> interviewCandidatesPage =
                interviewCandidatesService.selectInterviewCandidatesList(interviewCandidates, pageNum, pageSize);
        // 执行分页查询
        return Result.success(interviewCandidatesPage) ;
    }

//    /**
//     * 上传简历
//     * @param file
//     * @param candidateId
//     * @return
//     */
//    @PostMapping("/uploadResume")
//    public Result uploadResume(@RequestParam("file") MultipartFile file,
//                               @RequestParam("candidateId") Integer candidateId) {
//        try {
//            // 存储文件并获取路径
//            String filePath = resumeStorageService.storeResume(file, candidateId);
//
//            // 更新候选人简历路径
//            InterviewCandidates interviewCandidates = interviewCandidatesService.getById(candidateId);
////            InterviewCandidates candidate = new InterviewCandidates();
//            interviewCandidates.setId(candidateId);
//            interviewCandidates.setResumePath(filePath);
//            interviewCandidatesService.updateById(interviewCandidates);
//
//            return Result.success("简历上传成功", filePath);
//        } catch (IOException e) {
//            return Result.error("文件存储失败: " + e.getMessage());
//        } catch (IllegalArgumentException e) {
//            return Result.error(e.getMessage());
//        }
//    }

    /**
     * 文件上传OSS：
     * 阿里云 OSS 文件存储服务器
     * @param file
     * @return
     */
    @PostMapping("/uploadOss")
    public Result uploadOss(@RequestParam("file") MultipartFile file,
                            @RequestParam("candidateId") Integer candidateId){
        try {
            String uploadFile = ossUploadUtils.uploadFile(file, "resumes");
            InterviewCandidates candidate = interviewCandidatesService.getById(candidateId);
            if (candidate != null) {
                // 修改 resume_path
                candidate.setResumePath(uploadFile);
                // 更新候选人信息
                interviewCandidatesService.updateById(candidate);
            }
            // 获取文件的访问URL
            String fileUrl = ossUploadUtils.getFileUrl(uploadFile);
            return Result.success(fileUrl);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error("OSS 文件上传失败，请联系管理员");
        }
    }

    /**
     * 下载简历
     * @param path 简历路径
     * @return 下载URL
     */
    @GetMapping("/downloadResume")
    public Result downloadResume(@RequestParam("path") String path) {
        try {
            // 获取文件的下载URL
            String downloadUrl = ossUploadUtils.getFileUrl(path);
            return Result.success(downloadUrl);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取下载链接失败：" + e.getMessage());
        }
    }

}
